package _1_1_Basic;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by bpzj on 6/19/2018.
 */

public class Time {

    @Test
    public void timePattern() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        // 获取 时间 转为 String
        // 老方法
        String nowInOld = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

        // JDK8 方法
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        System.out.println(now);

        now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        System.out.println(now);

        Date date = new Date();


    }

    /**
     * 获取昨天 00:00:00 的 时间
     */
    @Test
    public void calendar() {
        // 1 使用calendar
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        Date time = calendar.getTime();
        String yes = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(time);
        System.out.println(yes);

        // 使用 LocalDateTime
        LocalDateTime yesterday = LocalDateTime.now().minus(1, ChronoUnit.DAYS);
        LocalDateTime time1 = yesterday.with(LocalTime.of(0, 0, 0));


    }

}
